package com.shamrock.cms.myrestful;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.shamrock.cms.entity.Admin;
import com.shamrock.cms.service.AdminService;
@Component
@Path("/users")
public class MyResource {
	@Autowired
    private AdminService adminService;
	
	/**
	 * 获取所有用户
	 * @return
	 */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Object getUsers() {
    	
        return adminService.getAllLists();
    }
    /**
     * 获取指定userid的用户
     * @param userid
     * @return
     */
    @GET
    @Path("/{userid}")  
    @Produces(MediaType.APPLICATION_JSON)
    public Object getUser(@PathParam("userid")Long userid){
    	Admin admin = adminService.getAdminById(userid);
    	if(admin!=null){
    		return admin;
    	}else{
    		return Response.status(Response.Status.NOT_FOUND)// 404
                    .entity("The user not found.").build();
    	}
    }
    /**
     * 以form形式传参创建user
     * @param name
     * @param password
     * @return
     */
    @POST
    @Consumes({ MediaType.APPLICATION_FORM_URLENCODED })
    @Produces({ MediaType.TEXT_HTML })
    @Transactional
    public Response addUser(@FormParam("name")String name,
    		@FormParam("password")String password){
    	Long adminId = adminService.addAdmin(name, password).getAdminId();

        return Response.status(Response.Status.CREATED)// 201
                .entity("A new user have created.")
                .header("Location",
                        "http://localhost:8899/myrestful/webapi/users/"
                                + String.valueOf(adminId)).build();
    }
    /**
     * 以json方式创建user
     * @param admin
     * @return
     */
    @POST
    @Consumes({ MediaType.APPLICATION_JSON})
    @Produces({ MediaType.TEXT_HTML })
    @Transactional
    public Response addUser(Admin admin){
    	Long adminId = adminService.addAdmin(admin.getName(),admin.getPassword()).getAdminId();

    	 return Response.status(Response.Status.CREATED)// 201
                 .entity("A new user have created.")
                 .header("Location",
                         "http://localhost:8899/myrestful/webapi/users/"
                                 + String.valueOf(adminId)).build();
    }
    /**
     * 更新用户密码
     * @param admin
     * @return
     */
    @PUT
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.TEXT_HTML })
    public Response updateUser(Admin admin){
    	adminService.updateAdminByName(admin.getName(), admin.getPassword());
    	return Response.status(Response.Status.ACCEPTED)
                .entity("password modified.").build();
    }
    /**
     * 删除用户
     * @param admin
     * @return
     */
    @DELETE
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.TEXT_HTML })
    public Response deleteUser(Admin admin){
    	adminService.delete(admin);
    	return Response.status(Response.Status.ACCEPTED)
                .entity("user deleted.").build();
    }
    
    /**
     * 删除用户
     * @param admin
     * @return
     */
    @DELETE
    @Path("/{userid}")  
    @Produces({MediaType.TEXT_HTML })
    public Response deleteUser(@PathParam("userid")Long userid){
    	adminService.deleteAdmin(userid);
    	return Response.status(Response.Status.ACCEPTED)
                .entity("user deleted.").build();
    }
    
}
